package com.yerbe.web.user.controller;

import com.alibaba.fastjson.JSON;
import com.yerbe.web.user.model.UserModel;
import com.yerbe.web.user.response.Result;
import com.yerbe.web.user.service.MongoUserService;
import com.yerbe.web.user.service.UserService;
import com.yerbe.web.user.utils.RedisUtils;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;
    private final MongoUserService mongoUserService;
    private final RedisUtils redisUtils;
    private final RestHighLevelClient restHighLevelClient;

    public UserController(UserService userService, MongoUserService mongoUserService,
                          RedisUtils redisUtils, RestHighLevelClient restHighLevelClient) {
        this.redisUtils = redisUtils;
        this.userService = userService;
        this.mongoUserService = mongoUserService;
        this.restHighLevelClient = restHighLevelClient;
    }

    @RequestMapping
    public String hello() {
        return "Hello Spring-Boot";
    }

    @RequestMapping("/info")
    public Map<String, Object> getInfo(@RequestParam String name) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("age", "unknown");

        ArrayList<HashMap<String, Object>> nameList = new ArrayList<>();
        nameList.add(map);
        nameList.add(map);
        nameList.add(map);
        return Result.data(nameList, "数据请求成功");
    }

    @RequestMapping("/newUser")
    public Map<String, Object> newUser(@RequestParam String name, @RequestParam String password, @RequestParam String phone) {
        int id = userService.newUser(name, password, phone);
        UserModel user = userService.findUserByPhone(phone);
        return Result.data(user, "数据请求成功");
    }

    @RequestMapping("/mongo/newUser")
    public Map<String, Object> newUserForMongo(@RequestParam String name, @RequestParam String password, @RequestParam String phone) {
        UserModel user = new UserModel();
        user.setName(name);
        user.setId(1);
        user.setPhone(phone);
        user.setPassword(password);
        mongoUserService.save(user);

        UserModel userModel = mongoUserService.findByPhone(phone);
        return Result.data(userModel, "数据请求成功");
    }

    @RequestMapping("/redis/test")
    public Map<String, Object> redis(@RequestParam String name) {

        redisUtils.set("name", name);
        String readFromRedis = redisUtils.get("name").toString();
        return Result.data(readFromRedis, "数据请求成功");
    }

    @RequestMapping("/es/test")
    public Map<String, Object> esTest(@RequestParam String name) {
        GetRequest getRequest = new GetRequest("name", name);
        try {
            GetIndexRequest request = new GetIndexRequest("name");
            boolean existsIndex = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
            if (!existsIndex) {
                CreateIndexRequest createIndexRequest = new CreateIndexRequest("name");
                restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
            }
            boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
            if (!exists) {
                IndexRequest indexRequest = new IndexRequest("name");
                indexRequest.id(name);
                UserModel userModel = new UserModel();
                userModel.setName(name);
                String json = JSON.toJSONString(userModel);
                indexRequest.source(json, XContentType.JSON);
                restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
            }else {
                UpdateRequest updateRequest = new UpdateRequest("name", name);
                updateRequest.doc("name", name);
                restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
            }
            GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
            return Result.data(getResponse, "数据请求成功");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.data("系统异常", "数据请求成功");
    }
}
